﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MpSoft.Library.DAO.Customers;
using MpSoft.Library.DTO;
using MpSoft.Basic.Configuration;

namespace MpSoft.Library.BUSS.Customers
{
    #region  Support
    public class SupportBUSS
    {
        private const string SUPPORT_ALL_KEY = "Mp.support.all-{0}";
        private const string SUPPORT_BY_ID_KEY = "Mp.mp_support.id-{0}";

        private static void RemoveCache()
        {
            MpCache.RemoveByPattern(SUPPORT_ALL_KEY);
            MpCache.RemoveByPattern(SUPPORT_BY_ID_KEY);
        }

        public static int Support_Add(Support obj)
        {
            var result = SupportDAO.Support_Add(obj);
            if (result > 0)
                RemoveCache();
            return result;
        }
        public static int Support_Update(Support obj)
        {
            var result = SupportDAO.Support_Update(obj);
            if (result > 0)
                RemoveCache();
            return result;
        }

        public static int Support_Delete(int SupportID)
        {
            var result = SupportDAO.Support_Delete(SupportID);
            if (result > 0)
                RemoveCache();
            return result;
        }

        public static Support Support_ByID(int SupportID)
        {
            string key = string.Format(SUPPORT_BY_ID_KEY, SupportID);
            var obj = MpCache.Get(key);
            if (obj != null)
                return (Support)obj;
            var list = Support_List(SupportID, -1, 0, 0);
            if (list != null)
            {
                var obj2 = list.FirstOrDefault();
                MpCache.Max(key, obj2);
                return obj2;
            }
            return null;
        }

        public static List<Support> Support_List( int IsActive, int SortOrder)
        {
            return Support_List(-1, IsActive, SortOrder);
        }

        public static List<Support> Support_List(int Type, int IsActive, int SortOrder)
        {
            string key = string.Format(SUPPORT_ALL_KEY, "t" + Type + "a" + IsActive + "s" + SortOrder);
            var obj = MpCache.Get(key);
            if (obj != null)
                return (List<Support>)obj;
            var list = Support_List(-1, Type, IsActive, SortOrder);
            MpCache.Max(key, list);
            return list;
        }

        public static List<Support> Support_List()
        {
            return Support_List(-1, -1, 0, 0);
        }

        public static List<Support> Support_List(int SupportID, int Type, int IsActive, int SortOrder)
        {
            return SupportDAO.Support_List(SupportID,  Type, IsActive, SortOrder);
        }
    }
    #endregion
    
}
